<?php

namespace app\admin\controller\expert;

use app\common\controller\Backend;
use fast\Tree;
use think\Cache;

/**
 *  占位关系管理
 *
 * @icon   fa fa-list
 * @remark 
 */
class Position extends Backend
{

    protected $searchFields = 'id,title';

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 列表
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list_info = db("expert_position ep")
                ->join('user u', "ep.user_id=u.id", "LEFT")
                ->field("ep.*,u.nickname,u.avatar,u.mobile,u.team_count")
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);
            $list = $list_info->items();
            $total = $list_info->total();

            $user_ids = [];
            $p_user_list = [];
            if ($list) {
                $p1_ids = array_column($list, "p1");
                $p2_ids = array_column($list, "p2");
                $p3_ids = array_column($list, "p3");
                $p4_ids = array_column($list, "p4");
                $p5_ids = array_column($list, "p5");
                $p6_ids = array_column($list, "p6");
                $p7_ids = array_column($list, "p7");
                $p8_ids = array_column($list, "p8");
                $p_all_ids = array_merge($p1_ids, $p2_ids, $p3_ids, $p4_ids, $p5_ids, $p6_ids, $p7_ids, $p8_ids);
                if ($p_all_ids) {
                    $p_all_ids = array_unique($p_all_ids);
                }
                if ($p_all_ids) {
                    $p_user_list = db("user")->whereIn("id", $p_all_ids)->column("*", "id");
                    // exit(print_r($p_user_list));
                }
            }
            foreach ($list as $k => $v) {
                // exit(print_r($v));

                $list[$k]["nickname1"] = isset($p_user_list[$v["p1"]]) ? $p_user_list[$v["p1"]]["nickname"] : "";
                $list[$k]["mobile1"] =  isset($p_user_list[$v["p1"]]) ? $p_user_list[$v["p1"]]["mobile"] : "";
                $list[$k]["team_count1"] = isset($p_user_list[$v["p1"]]) ? $p_user_list[$v["p1"]]["team_count"] : "";
              

                $list[$k]["nickname2"] =  isset($p_user_list[$v["p2"]]) ? $p_user_list[$v["p2"]]["nickname"] : "";
                $list[$k]["mobile2"] = isset($p_user_list[$v["p2"]]) ? $p_user_list[$v["p2"]]["mobile"] : "";
                $list[$k]["team_count2"] = isset($p_user_list[$v["p2"]]) ? $p_user_list[$v["p2"]]["team_count"] : "";

                $list[$k]["nickname3"] = isset($p_user_list[$v["p3"]]) ? $p_user_list[$v["p3"]]["nickname"] : "";
                $list[$k]["mobile3"] = isset($p_user_list[$v["p3"]]) ? $p_user_list[$v["p3"]]["mobile"] : "";
                $list[$k]["team_count3"] = isset($p_user_list[$v["p3"]]) ? $p_user_list[$v["p3"]]["team_count"] : "";

                $list[$k]["nickname4"] = isset($p_user_list[$v["p4"]]) ? $p_user_list[$v["p4"]]["nickname"] : "";
                $list[$k]["mobile4"] = isset($p_user_list[$v["p4"]]) ? $p_user_list[$v["p4"]]["mobile"] : "";
                $list[$k]["team_count4"] = isset($p_user_list[$v["p4"]]) ? $p_user_list[$v["p4"]]["team_count"] : "";

                $list[$k]["nickname5"] = isset($p_user_list[$v["p5"]]) ? $p_user_list[$v["p5"]]["nickname"] : "";
                $list[$k]["mobile5"] = isset($p_user_list[$v["p5"]]) ? $p_user_list[$v["p5"]]["mobile"] : "";
                $list[$k]["team_count5"] = isset($p_user_list[$v["p5"]]) ? $p_user_list[$v["p5"]]["team_count"] : "";

                $list[$k]["nickname6"] = isset($p_user_list[$v["p6"]]) ? $p_user_list[$v["p6"]]["nickname"] : "";
                $list[$k]["mobile6"] = isset($p_user_list[$v["p6"]]) ? $p_user_list[$v["p6"]]["mobile"] : "";
                $list[$k]["team_count6"] = isset($p_user_list[$v["p6"]]) ? $p_user_list[$v["p6"]]["team_count"] : "";

                $list[$k]["nickname7"] = isset($p_user_list[$v["p7"]]) ? $p_user_list[$v["p7"]]["nickname"] : "";
                $list[$k]["mobile7"] = isset($p_user_list[$v["p7"]]) ? $p_user_list[$v["p7"]]["mobile"] : "";
                $list[$k]["team_count7"] = isset($p_user_list[$v["p7"]]) ? $p_user_list[$v["p7"]]["team_count"] : "";

                $list[$k]["nickname8"] = isset($p_user_list[$v["p8"]]) ? $p_user_list[$v["p8"]]["nickname"] : "";
                $list[$k]["mobile8"] = isset($p_user_list[$v["p8"]]) ? $p_user_list[$v["p8"]]["mobile"] : "";
                $list[$k]["team_count8"] = isset($p_user_list[$v["p8"]]) ? $p_user_list[$v["p8"]]["team_count"] : "";

                $list[$k]["avatar"] = $v["avatar"] ? cdnurl($v["avatar"], true) : letter_avatar($v["nickname"]);
                unset($list[$k]["password"]);
                unset($list[$k]["salt"]);
            }
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }
}
